require(['jquery','underscore','api','laypage'], function ($,_,api,laypage) {
  _.templateSettings = _templateSettings;

  //条件
  $.get(api.baseUrl+api.designer.options, function (res) {
    console.log(res);
    var data = res.data;
    $('.filter').append(_.template($('#options-tpl').html())({data: data}));
  });

  //案例列表
  var designerUrl = api.baseUrl+api.designer.list;
  var option = {
    style: "",
    title: "",
    sorts: "r",

  };
  getDesignerList(designerUrl);

  $(".filter").on("click","#title .item", function () {
    $(this).addClass("active").siblings().removeClass("active");
    var code = $(this).data("code");
    option.title = code;

    getDesignerList(designerUrl);
  });

  $(".filter").on("click","#styles .item", function () {
    $(this).addClass("active").siblings().removeClass("active");
    var code = $(this).data("code");
    option.style = code;

    getDesignerList(designerUrl);
  });

  var sortArr = new Array(3);
  $(".filter").on("click",".sort", function () {
    var sorts = $(this).data("sorts"),idx =$(this).index();
    $(this).toggleClass("active");
    sortArr[idx] = sorts;

    if($(this).hasClass("active")){
      $(this).data("sorts",sorts.toUpperCase())
    }else{
      $(this).data("sorts",sorts.toLowerCase())
    }

    var temArr = [];
    _.each(sortArr, function (item) {
      if(item){
        temArr.push(item);
      }
    });
    option.sorts = temArr.join(",");
    getDesignerList(designerUrl);
  });

  //function getDesignerList(url) {
  //  $.get(url,option,function (res) {
  //    var data = res.data,
  //        pageInfo = res.page;
  //    $(".main .row").empty().append(_.template($("#designer-item-tpl").html())({data: data}));
  //
  //  });
  //}

    function getDesignerList(url){
    $.get(url,option,function (res) {
      var data = res.data,
        pageInfo = res.page;
      laypage({
        cont: $('.pagination'), //容器。值支持id名、原生dom对象，jquery对象,
        curr: 1,
        pages: pageInfo.pct, //总页数
        skip: true, //是否开启跳页
        skin: '#26aff5',
        groups: 6, //连续显示分页数
        first: 1, //将首页显示为数字1,。若不显示，设置false即可
        last: pageInfo.pct, //将尾页显示为总页数。若不显示，设置false即可
        prev: '<<', //若不显示，设置false即可
        next: '>>', //若不显示，设置false即可
        jump: function (obj, first) {
          if(!first){
            $(".main .row").empty().append(_.template($("#designer-item-tpl").html())({data: data}));
          }else{
            $(".main .row").empty().append(_.template($("#designer-item-tpl").html())({data: data}));
          }
          //if(!first){
          //  params.pageNum = obj.curr;
          //  _this.requestHttp(_this.url, params, function (data) {
          //    if(_this.type=="get"){
          //      _this.render('tableList', data);
          //    }
          //    _this.render('tableList', data.data);
          //  });
          //}else{
          //  if(_this.type=="get"){
          //    _this.render('tableList', data);
          //  }
          //  _this.render('tableList', data.data);
          //}
        }
      })
    });
  }

});